<template>
  <MainView page-title="登录">
    <div class="index-view" direction="horizontal">
      <el-form
        class="login-form"
        :model="loginData"
        ref="loginForm"
        :rules="loginRule"
        label-width="80px"
        :inline="false"
        size="normal"
      >
        <el-form-item label="账号：" prop="username">
          <el-autocomplete
            v-model="loginData.username"
            value-key=""
            placeholder="请输入账号！"
            clearable
            :fetch-suggestions="querySearch"
            @select="handleSelect"
          >
            <template slot-scope="{ item }">
              <div class="username">{{ item.username }}</div>
              <span class="password">{{ item.password.replace(/./g, '*') }}</span>
            </template>
          </el-autocomplete>
        </el-form-item>
        <el-form-item label="密码：" prop="password">
          <el-input
            v-model="loginData.password"
            placeholder="请输入密码！"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit" :loading="loginFlag">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </MainView>
</template>

<script>
  import MainView from '@/components/MainView'
  export default {
    components: {
      MainView
    },
    data() {
      return {
        loginData: {
          username: '',
          password: ''
        },
        loginRule: {
          username: [{ require: true, message: '请输入账号！', trigger: 'blur' }],
          password: [{ require: true, message: '请输入密码！', trigger: 'blur' }]
        },
        localData: [
          { username: 'aaaa', password: '123456' },
          { username: 'bbbb', password: '654321' }
        ],
        loginFlag: false
      }
    },
    methods: {
      onSubmit() {
        this.$refs.loginForm.validate(val => {
          if (val) {
            this.loginFlag = true
            setTimeout(() => {
              this.loginFlag = false
              sessionStorage.setItem('username', this.loginData.username)
              location.href = 'http://localhost:8080'
            }, 500)
          }
        })
      },
      querySearch(query, cb) {
        console.log(query, cb)
        cb(this.localData)
      },
      handleSelect(item) {
        this.loginData.username = item.username
        this.loginData.password = item.password
      }
    }
  }
</script>
<style lang="scss" scoped>
  .index-view {
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  /deep/ .el-input {
    width: 200px;
  }
  .username {
    font-size: 18px;
  }
  .password {
    font-size: 12px;
  }
</style>
